package com.carey.blog.action.impl;

import static com.carey.blog.model.Article.ARTICLE_ABSTRACT;
import static com.carey.blog.model.Article.ARTICLE_AUTHOR_EMAIL;
import static com.carey.blog.model.Article.ARTICLE_COMMENT_COUNT;
import static com.carey.blog.model.Article.ARTICLE_CONTENT;
import static com.carey.blog.model.Article.ARTICLE_CREATE_DATE;
import static com.carey.blog.model.Article.ARTICLE_RANDOM_DOUBLE;
import static com.carey.blog.model.Article.ARTICLE_TAGS_REF;
import static com.carey.blog.model.Article.ARTICLE_UPDATE_DATE;
import static com.carey.blog.model.Article.ARTICLE_VIEW_COUNT;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.carey.blog.framework.latke.Keys;
import com.carey.blog.framework.latke.action.AbstractAction;
import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.model.Common;
import com.carey.blog.model.Preference;
import com.carey.blog.repository.ArticleRepository;
import com.carey.blog.repository.impl.ArticleGAERepository;
import com.carey.blog.util.Preferences;

/**
 * Gets random articles action.
 * 
 */
public final class GetRandomArticlesAction extends AbstractAction {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(GetRandomArticlesAction.class.getName());
	/**
	 * Article repository.
	 */
	private ArticleRepository articleRepository = ArticleGAERepository
			.getInstance();
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();

	@Override
	protected Map<?, ?> doFreeMarkerAction(
			final freemarker.template.Template template,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	protected JSONObject doAjaxAction(final JSONObject data,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final List<JSONObject> randomArticles = getRandomArticles();
		final JSONObject ret = new JSONObject();

		try {
			ret.put(Common.RANDOM_ARTICLES, randomArticles);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		}

		return ret;
	}

	/**
	 * Gets the random articles.
	 * 
	 * @return a list of articles, returns an empty list if not found
	 * @throws ActionException
	 *             action exception
	 */
	public List<JSONObject> getRandomArticles() throws ActionException {
		try {
			final JSONObject preference = preferenceUtils.getPreference();
			if (null == preference) {
				throw new ActionException("Not found preference");
			}

			final int displayCnt = preference
					.getInt(Preference.RANDOM_ARTICLES_DISPLAY_CNT);
			final List<JSONObject> ret = articleRepository
					.getRandomly(displayCnt);

			// Remove unused properties
			for (final JSONObject article : ret) {
				article.remove(Keys.OBJECT_ID);
				article.remove(ARTICLE_AUTHOR_EMAIL);
				article.remove(ARTICLE_ABSTRACT);
				article.remove(ARTICLE_COMMENT_COUNT);
				article.remove(ARTICLE_CONTENT);
				article.remove(ARTICLE_CREATE_DATE);
				article.remove(ARTICLE_TAGS_REF);
				article.remove(ARTICLE_UPDATE_DATE);
				article.remove(ARTICLE_VIEW_COUNT);
				article.remove(ARTICLE_RANDOM_DOUBLE);
			}

			return ret;
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ActionException(e);
		}
	}
}
